home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / ttyname.c < prev    next >
C/C++ Source or Header  |  1993-11-02  |  2KB  |  101 lines

  1. /*
  2.  * ttyname -- figure out the name of the terminal attached to a file
  3.  * descriptor
  4.  *
  5.  * Written by Eric R. Smith and placed in the public domain.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <types.h>
  10. #include <stat.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <mintbind.h>
  14. #include <limits.h>
  15. #include <errno.h>
  16. #include <ioctl.h>
  17. #include "lib.h"
  18.  
  19. extern int __mint;
  20.  
  21. static int find_ino __PROTO((char *, struct stat *, char *));
  22.  
  23. static char tname[L_ctermid];
  24.  
  25. /* Find the file in directory "dir" that matches "sbuf". Returns 1
  26.  * on success, 0 on failure. Note that "dir" is a prefix, i.e. it
  27.  * should end with "\\".
  28.  */
  29.  
  30. static int
  31. find_ino(dir, sb, name)
  32.     char *dir;
  33.     struct stat *sb;
  34.     char *name;
  35. {
  36.     char _name[PATH_MAX];
  37.     char *where = _name;
  38.     long drv;
  39.     struct stat testsb;
  40.     struct dbuf {
  41.         long ino;
  42.         char name[NAME_MAX + 1];
  43.     } dbuf;
  44.  
  45.     drv = Dopendir (dir, 0);
  46.     if ((drv & 0xff000000L) == 0xff000000L) return 0;
  47.  
  48.     while (*dir) {
  49.         *where++ = *dir++;
  50.     }
  51.  
  52.     while (Dreaddir((int) sizeof (dbuf), drv, (char *) &dbuf) == 0) {
  53.         strcpy(where, dbuf.name);
  54.         if (Fxattr(0, _name, &testsb))
  55.             continue;
  56.         if (testsb.st_dev == sb->st_dev &&
  57.             testsb.st_ino == sb->st_ino) {
  58.             Dclosedir(drv);
  59.             _dos2unx (_name, name);
  60.             return 1;
  61.         }
  62.     }
  63.     Dclosedir(drv);
  64.     return 0;
  65. }
  66.  
  67. char *
  68. ttyname(fd)
  69.     int fd;
  70. {
  71.     char *name;
  72.     struct stat sb;
  73.  
  74.     if (!isatty(fd)) return (char *)0;
  75.  
  76.     if (__mint < 9) {
  77.         if (fd == -2) {
  78.             name = "/dev/aux";
  79.         } else {
  80.             name = "/dev/con";
  81.         }
  82.         strcpy(tname, name);
  83.         return tname;
  84.     }
  85.  
  86.     if (Fcntl(fd, &sb, FSTAT))
  87.         return (char *)0;
  88.  
  89.     /* try the devices first */
  90.     if (find_ino("u:\\dev\\", &sb, tname))
  91.         return tname;
  92.  
  93.     /* hmmm, maybe we're a pseudo-tty */
  94.     if (find_ino("u:\\pipe\\", &sb, tname))
  95.         return tname;
  96.  
  97.     /* I give up */
  98.     strcpy(tname, "/dev/tty");
  99.     return tname;
  100. }
  101.